﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media; 

namespace GameHungTrung
{
    public class GamePlayScreen : AbstractScreen
    {
        Basket _roObject;

        // Ga vit ngang
        List<Animal> listAnimal = null;
        Animal chickenPrototype, duckPrototype, goosePrototype;

        SpriteFont font;

        Texture2D heart;
        Texture2D rope;

        Texture2D background;

        bool isPressed_G = false, isPressed_V = false, isPressed_N = false;

        public GamePlayScreen()
        {
            _roObject = new Basket();
            listAnimal = new List<Animal>();
            chickenPrototype = new Chicken();
            duckPrototype = new Duck();
            goosePrototype = new Goose();
        }

        public override void LoadContent(ContentManager content)
        {
            _roObject.LoadContent(content);
            chickenPrototype.LoadContent(content);
            duckPrototype.LoadContent(content);
            goosePrototype.LoadContent(content);

            font = content.Load<SpriteFont>(@"Font/Score");

            
            heart = content.Load<Texture2D>(Global.PATH_FILE_IMAGE + "heart");

            rope = content.Load<Texture2D>(Global.PATH_FILE_IMAGE + "rope");           

            background = content.Load<Texture2D>(@"Images\background");
        }

        TimeSpan timespan = TimeSpan.FromMilliseconds(1000);
        public override void Update(GameTime gameTime)
        {
            if (Global.GAME_STATE == Global.GAMESTATE_ENUM.START)
            {
                if (Global.MODE_GAME.CompareTo("EASY") == 0)
                {
                    KeyboardState ks = Keyboard.GetState();

                    //// Ga
                    if (ks.IsKeyDown(Keys.G) && isPressed_G == false)
                    {
                        isPressed_G = true;
                        listAnimal.Add(chickenPrototype.Clone());
                    }
                    if (ks.IsKeyUp(Keys.G))
                    {
                        if (isPressed_G)
                            isPressed_G = false;
                    }

                    //// vit
                    if (ks.IsKeyDown(Keys.V) && isPressed_V == false)
                    {

                        isPressed_V = true;
                        listAnimal.Add(duckPrototype.Clone());
                    }
                    if (ks.IsKeyUp(Keys.V))
                    {
                        if (isPressed_V)
                            isPressed_V = false;
                    }

                    // Ngan
                    if (ks.IsKeyDown(Keys.N) && isPressed_N == false)
                    {
                        isPressed_N = true;
                        listAnimal.Add(goosePrototype.Clone());
                    }
                    if (ks.IsKeyUp(Keys.N))
                    {
                        if (isPressed_N)
                            isPressed_N = false;
                    }
                }
                if (Global.MODE_GAME.CompareTo("HARD") == 0)
                {
                    if (listAnimal.Count <= Global.MAX_ANIMAL)
                    {
                        timespan -= gameTime.ElapsedGameTime;
                        if (timespan <= TimeSpan.Zero)
                        {
                            Random r = new Random();
                            int index = r.Next(0, 3);
                            switch (index)
                            {
                                case 0:
                                    listAnimal.Add(chickenPrototype.Clone());
                                    break;
                                case 1:
                                    listAnimal.Add(duckPrototype.Clone());
                                    break;
                                case 2:
                                    listAnimal.Add(goosePrototype.Clone());
                                    break;
                            }
                            index = r.Next(1, Global.MAX_TIME_RANDOM_ANIMAL);
                            timespan = TimeSpan.FromMilliseconds(1000 * index);
                        }
                    }
                }
                _roObject.Update(gameTime);

                for (int i = 0; i < listAnimal.Count; i++)
                {
                    listAnimal[i].UpdateRegionBasket(_roObject.XY_RegionCollision, _roObject.WH_RegionCollision);
                    listAnimal[i].Update(gameTime);
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, new Vector2(0, 0), Color.White);

            _roObject.Draw(gameTime, spriteBatch);

            spriteBatch.Draw(rope, new Vector2(0,Global.Y_DAY_TREN ), Color.White);
            spriteBatch.Draw(rope, new Vector2(0, Global.Y_DAY_DUOI), Color.White);

            for (int i = 0; i < listAnimal.Count; i++)
            {
                listAnimal[i].Draw(gameTime, spriteBatch);
            }

            spriteBatch.DrawString(font, "Shit: " + Global.NUMBER_SHIT.ToString() +
                "  Score: " + Global.NUMBER_EGG.ToString(),
                        new Vector2(Global.SCREEN_WIDTH/2, 20),
                        Color.White);

            for (int i = 0; i < Global.NUMBER_HEART; i++)
            {
                spriteBatch.Draw(heart, new Vector2(10 + i * heart.Width, 10), Color.White);
            }


            spriteBatch.DrawString(font, "Blackatist", 
                new Vector2(Global.SCREEN_WIDTH*2/3, Global.SCREEN_HEIGHT - Global.SCREEN_HEIGHT/10), Color.White);

            spriteBatch.DrawString(font, "Mode: " + Global.MODE_GAME.ToString(),
                new Vector2(Global.SCREEN_WIDTH / 10, Global.SCREEN_HEIGHT - Global.SCREEN_HEIGHT / 10), Color.White);
        }

        public void ResetGame()
        {
            Global.NUMBER_EGG = 0;
            Global.NUMBER_HEART = 3;
            Global.NUMBER_SHIT = 0;

            for (int i = 0; i < listAnimal.Count; i++)
                listAnimal[i].Reset();
            listAnimal.RemoveRange(0,listAnimal.Count);
            listAnimal = new List<Animal>();
        }
    }
}
